import {
    mapState,
    mapGetters,
} from 'vuex'

export default {
    computed: {
        ...mapState('user', ['token']),
        ...mapGetters('cart', ['goodsCount']),
    },
    watch: {
        //监听goodsCount的变化
        goodsCount(newVal) {
            if (newVal === undefined || newVal === '') {
                return
            }
            uni.setTabBarBadge({
                index: 2, //tab页面所在的位置的索引
                text: this.goodsCount + '' //显示的角标内容，必须是字符串
            })
        }
    },
    async onShow() {
        //如果登陆了，就获取购物车信息，显示角标
        if (this.token) {
            await this.$store.dispatch('cart/getInitData')
            //setTabBarBadge设置tab栏角标
            uni.setTabBarBadge({
                index: 2, //tab页面所在的位置的索引
                text: this.goodsCount + '' //显示的角标内容，必须是字符串
            })
        }

    }

}
